const app = getApp();
const ajax = app.myRequest()

Page({

    data: {},

    onLoad: function (options) {
        var status = options.status
        var id = options.id
        this.setData({ status, id })
    },

    getDetails() {
        var status = this.data.status
        var id = this.data.id
        var _this = this
        ajax.getRequest('/Order/index', { status, id }).then((res) => {
            _this.setData({ hot: res.data.data })
        })
    },

    // 点击支付
    payment() {
        var id = this.data.id
        var _this = this
        wx.requestSubscribeMessage({
            tmplIds: ['kvmaVk0OuBOwrZKjTiMJ1je4JK6OeBzrtShS9lqakjU'],
            success(res) {
                if (res['kvmaVk0OuBOwrZKjTiMJ1je4JK6OeBzrtShS9lqakjU'] === 'accept') {
                    ajax.postRequest('/Order/payAgain', { id }).then((res) => {
                        if (res.data.code == 1) {
                            _this.generateOrder(res.data.data)
                        } else {
                            wx.showModal({ showCancel: false, title: '提示', content: res.data.msg })
                        }
                    })
                }
            }
        })
    },
    
    // 发起支付
    generateOrder(res) {
        wx.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success: function (res) {
                wx.showToast({ title: '支付成功', mask: true, icon: 'success' })
                wx.navigateBack()
            },
            fail: function (res) {
                wx.showToast({ title: '支付失败', mask: true, icon: 'none' })
            }
        })
    },

    onShow: function () {
        this.getDetails()
    },
})